### REPLICATION 
### Business Owners and Executives as Politicians: The Effect on Public Policy
### this script produces RD plots, results plots, and tables located in Supplemental Information


rm(list=ls(all=TRUE))

library(tidyverse)
library(scales)
library(coefplot)
library(xtable)

## set working directory
setwd("")

source("replication_clean_results.R")
source("replication_bin_data_rd_plots.R")
source("replication_mayors_source.R")


#### Section A - Validity of RDD

## Figure A.1
## generate figure
hist_forcing_var <- ggplot(mayors_subs, aes(exec_margin)) + 
  geom_histogram(binwidth = .02, colour = "black", alpha = .5, boundary = .5) +
  coord_cartesian(xlim = c(-.3, .3)) +
  scale_y_continuous(limits = c(0, 60)) +
  geom_vline(xintercept = 0, colour = "grey", linetype = "dashed") +
  xlab("Business Executive Margin") + 
  ylab("Count") +
  theme_bw()

## return figure
hist_forcing_var

## save figure
ggsave(file = "fig_a1_replication_hist_forcing_var_RR.pdf", hist_forcing_var, height = 6, width = 6)

## Table A.1 (Supplemental Information)
print.xtable(xtable(select(results_balance_all_wide, coef_CCT, robust_se_CCT, robust_pval_CCT, h_CCT, coef_05, robust_se_05, robust_pval_05, h_05),
                    digits = c(3, 3, 3, 3, 0, 3, 3, 3, 0)),
             hline.after = NULL,
             only.contents = TRUE, include.colnames = FALSE, include.rownames = TRUE,
             add.to.row = list(pos = list(1:26), command = c("\\\\\n")),
             file = "tab_a1_replication_table_results_balance_covs_all_RR.tex")


## Table A.2
print.xtable(xtable(select(results_balance_rdrobust_covs, coef, ci, p_val, h), digits = c(3, 3, 3, 3, 3)), hline.after = NULL,
             only.contents = TRUE, include.colnames = FALSE, include.rownames = TRUE,
             add.to.row = list(pos = list(1:26), command = c("\\\\\n")),
             file = "tab_a2_replication_table_balance_results_rdrobust_covs_RR.tex")

#### Section B - Main results

## Figure B.2(a)
## generate figure
plot_revs_binned_RR <- ggplot(binned_revenue, aes(x = binned_margin, y = binned_rev, group = binned_margin > 0)) +
  geom_point(alpha = .5) +
  geom_smooth(method = "lm", se = FALSE) +
  geom_vline(xintercept = 0, color = "red", linetype = "dashed") +
  scale_x_continuous(limits = c(-.25, .25)) +
  scale_y_continuous(limits = c(500, 3000), breaks = seq(500, 2500, 500), labels = dollar) +
  xlab("Business Executive Vote Share Margin") +
  ylab("Total Revenue (per capita)") +
  theme_bw() +
  theme(axis.title.x  = element_text(size=16, margin = margin(t = 16)),
        axis.text.x  = element_text(size=12),
        axis.title.y  = element_text(size=16, margin = margin(r = 16)),
        axis.text.y  = element_text(size=12),
        legend.position = "none") 

## return figure
plot_revs_binned_RR

## save figure
ggsave(plot_revs_binned_RR, file = "fig_b2a_replication_plot_revs_binned_RR.pdf", width = 10, height = 8)

## Figure B.2(b)
## generate figure
plot_exps_binned_RR <- plot_revs_binned_RR %+% 
  binned_expenditure %+% aes(x = binned_margin, y = binned_exp, group = binned_margin > 0) +
  xlab("Business Executive Vote Share Margin") +
  ylab("Total Expenditure (per capita)") 

## return figure
plot_exps_binned_RR

## save figure
ggsave(plot_exps_binned_RR, file = "fig_b2b_replication_plot_exps_binned_RR.pdf", width = 10, height = 8)


## Table B.3 (Supplemental Information)
## size of govt
print.xtable(xtable(select(filter(gg_results_CCT, specs == "CCT covs", outcome_class == "size"), 
                           outcome, coef, robust_se, robust_pval, h, n), digits = c(0, 3, 3, 3, 3, 3, 0)), hline.after = NULL,
             only.contents = TRUE, include.colnames = FALSE, include.rownames = FALSE,
             add.to.row = list(pos = list(1:9), command = c("\\\\\n")),
             file = "tab_b3_replication_table_results_size_CCT_RR.tex")

## Table B.4 (Supplemental Information)
## spending
print.xtable(xtable(select(filter(gg_results_CCT, specs == "CCT covs", (outcome_class == "spending" | outcome_class == "spending category")), 
                           outcome, coef, robust_se, robust_pval, h, n), digits = c(0, 3, 3, 3, 3, 3, 0)), hline.after = NULL,
             only.contents = TRUE, include.colnames = FALSE, include.rownames = FALSE,
             add.to.row = list(pos = list(1:12), command = c("\\\\\n")),
             file = "tab_b4_replication_table_results_spending_CCT_RR.tex")


#### Section C - Size of govt 

## Table C.5
print.xtable(xtable(select(filter(tab_alt_specs_quad, outcome_class == "size", specs == "CCT covs"), outcome, coef, robust_se, robust_pval, h),
                    digits = c(3, 3, 3, 3, 3, 3)), hline.after = NULL,
             only.contents = TRUE, include.colnames = FALSE, include.rownames = FALSE,
             add.to.row = list(pos = list(1:9), command = c("\\\\\n")),
             file = "tab_c5_replication_table_quadratic_results_size_RR.tex")


## Table C.6
print.xtable(xtable(select(tab_alt_specs_size, outcome, base_coef, base_robust_se, base_robust_pval, base_h,
                           covs_coef, covs_robust_se, covs_robust_pval, covs_h, dv_form),
                    digits = c(3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0)), hline.after = NULL,
                 only.contents = TRUE, include.colnames = FALSE, include.rownames = FALSE,
             add.to.row = list(pos = list(seq(3, 27, 3)), command = c("\\\\\n")),
             file = "tab_c6_replication_table_results_alt_specs_size_RR.tex")


## Figure C.3(a)
## generate figures
plot_ttl_own_revs_multi <- ggplot(filter(gg_multi_bandwidth, outcome == "ttl_own_revs",  dv_form == "Dollars (per capita, logged)"), aes(x = h, y = coef)) +
  geom_point(size = 2, alpha = .5) +
  geom_errorbar(aes(ymin = coef - (-qnorm((1-0.90)/2) * robust_se), ymax = coef + (-qnorm((1-0.90)/2) * robust_se)), width = 0, size = 1, alpha = .6) +
  geom_errorbar(aes(ymin = coef - (-qnorm((1-0.95)/2) * robust_se), ymax = coef + (-qnorm((1-0.95)/2) * robust_se)), width = 0, linetype = "dotted", alpha = .3) +
  facet_grid(. ~ dv_type, scales = "free_x", space = "free_x") +
  geom_hline(aes(yintercept=0), linetype="dashed", colour="dark grey") +
  scale_x_continuous(limits=c(.01, .25), breaks=seq(.05, .25, .05)) +
  xlab("Bandwidth") + 
  ggtitle("") +
  theme_bw() +
  theme(axis.title.x = element_text(size = 16),
        axis.text.x = element_text(size = 12),
        axis.text.y = element_text(size = 12),
        strip.text.y = element_text(size = 12))


plot_ttl_own_revs_multi_logs <- plot_ttl_own_revs_multi %+%
  filter(gg_multi_bandwidth, outcome == "ttl_own_revs",  dv_form == "Dollars (per capita, logged)") +
  geom_point(data = filter(gg_rdrobust, outcome == "ttl_own_revs", dv_form == "Dollars (per capita, logged)"), aes(x = h, y = coef), shape = 5, size = 2.5) +
  geom_errorbar(data = filter(gg_rdrobust, outcome == "ttl_own_revs", dv_form == "Dollars (per capita, logged)"),
                aes(ymin = ci_low, ymax = ci_upp), width = 0, linetype = "dashed") +
  scale_y_continuous( breaks = seq(-1, 2, 1), labels = scale_fun_revs) +
  coord_cartesian(ylim = c(-1.5, 2.5)) +
  ylab("Effect on Own-source Revenue \n(per capita, logged)") 


plot_ttl_own_revs_multi_dollars <- plot_ttl_own_revs_multi %+%
  filter(gg_multi_bandwidth, outcome == "ttl_own_revs",  dv_form == "Dollars (per capita)") +
  geom_point(data = filter(gg_rdrobust, outcome == "ttl_own_revs", dv_form == "Dollars (per capita)"), aes(x = h, y = coef), shape = 5, size = 2.5) +
  geom_errorbar(data = filter(gg_rdrobust, outcome == "ttl_own_revs", dv_form == "Dollars (per capita)"),
                aes(ymin = ci_low, ymax = ci_upp), width = 0, linetype = "dashed") +
  scale_y_continuous( breaks = seq(-200, 1200, 200), labels = dollar) +
  coord_cartesian(ylim = c(-250, 1250)) +
  ylab("Effect on Own-source Revenue \n (per capita)") 


plot_ttl_own_revs_multi_diffs <- plot_ttl_own_revs_multi %+%
  filter(gg_multi_bandwidth, outcome == "ttl_own_revs",  dv_form == "Diffference in Dollars (per capita)") +
  geom_point(data = filter(gg_rdrobust, outcome == "ttl_own_revs", dv_form == "Diffference in Dollars (per capita)"), aes(x = h, y = coef), shape = 5, size = 2.5) +
  geom_errorbar(data = filter(gg_rdrobust, outcome == "ttl_own_revs", dv_form == "Diffference in Dollars (per capita)"),
                aes(ymin = ci_low, ymax = ci_upp), width = 0, linetype = "dashed") +
  scale_y_continuous( breaks = seq(-200, 1200, 200), labels = dollar) +
  coord_cartesian(ylim = c(-250, 1250)) +
  ylab("Effect on Own-source Revenue \n(per capita difference)")

## return figures
plot_ttl_own_revs_multi_dollars
plot_ttl_own_revs_multi_diffs
plot_ttl_own_revs_multi_logs

## save figures
ggsave(plot_ttl_own_revs_multi_logs, filename = "fig_c3a_replication_plot_ttl_own_revs_multi_logs.pdf", height = 4, width = 12)
ggsave(plot_ttl_own_revs_multi_dollars, filename = "fig_c3a_replication_plot_ttl_own_revs_multi_dollars.pdf", height = 4, width = 12)
ggsave(plot_ttl_own_revs_multi_diffs, filename = "fig_c3a_replication_plot_ttl_own_revs_multi_diffs.pdf", height = 4, width = 12)


## Figure C.3(b)
## generate figures
plot_ttl_chgs_miscrev_multi_logs <- plot_ttl_own_revs_multi %+%
  filter(gg_multi_bandwidth, outcome == "ttl_chgs_miscrev",  dv_form == "Dollars (per capita, logged)") +
  geom_point(data = filter(gg_rdrobust, outcome == "ttl_chgs_miscrev", dv_form == "Dollars (per capita, logged)"), aes(x = h, y = coef), shape = 5, size = 2.5) +
  geom_errorbar(data = filter(gg_rdrobust, outcome == "ttl_chgs_miscrev", dv_form == "Dollars (per capita, logged)"),
                aes(ymin = ci_low, ymax = ci_upp), width = 0, linetype = "dashed") +
  scale_y_continuous( breaks = seq(-1, 2, 1), labels = scale_fun_revs) +
  coord_cartesian(ylim = c(-1.5, 2.5)) +
  ylab("Effect on Charges & Misc. Revenue \n(per capita, logged)") 


plot_ttl_chgs_miscrev_multi_dollars <- plot_ttl_own_revs_multi %+%
  filter(gg_multi_bandwidth, outcome == "ttl_chgs_miscrev",  dv_form == "Dollars (per capita)") +
  geom_point(data = filter(gg_rdrobust, outcome == "ttl_chgs_miscrev", dv_form == "Dollars (per capita)"), aes(x = h, y = coef), shape = 5, size = 2.5) +
  geom_errorbar(data = filter(gg_rdrobust, outcome == "ttl_chgs_miscrev", dv_form == "Dollars (per capita)"),
                aes(ymin = ci_low, ymax = ci_upp), width = 0, linetype = "dashed") +
  scale_y_continuous( breaks = seq(-200, 1200, 200), labels = dollar) +
  coord_cartesian(ylim = c(-250, 1250)) +
  ylab("Effect on Charges & Misc. Revenue \n(per capita)") 


plot_ttl_chgs_miscrev_multi_diffs <- plot_ttl_own_revs_multi %+%
  filter(gg_multi_bandwidth, outcome == "ttl_chgs_miscrev",  dv_form == "Diffference in Dollars (per capita)") +
  geom_point(data = filter(gg_rdrobust, outcome == "ttl_chgs_miscrev", dv_form == "Diffference in Dollars (per capita)"), aes(x = h, y = coef), shape = 5, size = 2.5) +
  geom_errorbar(data = filter(gg_rdrobust, outcome == "ttl_chgs_miscrev", dv_form == "Diffference in Dollars (per capita)"),
                aes(ymin = ci_low, ymax = ci_upp), width = 0, linetype = "dashed") +
  scale_y_continuous( breaks = seq(-200, 1200, 200), labels = dollar) +
  coord_cartesian(ylim = c(-250, 1250)) +
  ylab("Effect on Charges & Misc. Revenue \n(per capita difference)")



## return figures
plot_ttl_chgs_miscrev_multi_dollars
plot_ttl_chgs_miscrev_multi_diffs
plot_ttl_chgs_miscrev_multi_logs

## save figures
ggsave(plot_ttl_chgs_miscrev_multi_logs, filename = "fig_c3b_replication_plot_ttl_chgs_miscrev_multi_logs.pdf", height = 4, width = 12)
ggsave(plot_ttl_chgs_miscrev_multi_dollars, filename = "fig_c3b_replication_plot_ttl_chgs_miscrev_multi_dollars.pdf", height = 4, width = 12)
ggsave(plot_ttl_chgs_miscrev_multi_diffs, filename = "fig_c3b_replication_plot_ttl_chgs_miscrev_multi_diffs.pdf", height = 4, width = 12)




###  Debt plots & table 
## Figure C.4(a)
## generate figure
rd_plot_debt <- ggplot(mayors_subs, aes(x = exec_margin, y = pc_ttl_debt_lead2, group = exec_margin > 0)) +
  geom_point(shape = 1, size = .75, alpha = .25) +
  stat_summary_bin(binwidth = .02, fun = "mean", geom = "point", size = 1) +
  geom_smooth(method = "lm", se = FALSE) +
  geom_vline(xintercept = 0, color = "red", linetype = "dashed") +
  scale_x_continuous(limits = c(-.25, .25)) +
  scale_y_continuous(limits = c(0, 10000), breaks = seq(0, 10000, 2500), labels = dollar) +
  xlab("Business Executive \nVote Share Margin") +
  ylab("Total Debt \n(per capita)") +
  theme_bw() +
  theme(axis.title.x  = element_text(size=16, margin = margin(t = 16)),
        axis.text.x  = element_text(size=12),
        axis.title.y  = element_text(size=16, margin = margin(r = 16)),
        axis.text.y  = element_text(size=12),
        legend.position = "none") 


## return figure
rd_plot_debt

## save figure
ggsave(rd_plot_debt, filename = "fig_c4a_replication_rd_plot_debt.pdf", width = 5, height = 4)

## Figure C.4(b)
## generate figure
rd_plot_debt_log <- ggplot(mayors_subs, aes(x = exec_margin, y = log(pc_ttl_debt_lead2 + 1), group = exec_margin > 0)) +
  geom_point(shape = 1, size = .75, alpha = .25) +
  stat_summary_bin(binwidth = .02, fun = "mean", geom = "point", size = 1) +
  geom_smooth(method = "lm", se = FALSE) +
  geom_vline(xintercept = 0, color = "red", linetype = "dashed") +
  scale_x_continuous(limits = c(-.25, .25)) +
  scale_y_continuous(limits = c(0, 10), breaks = seq(0, 10, 2.5), labels = dollar) +
  xlab("Business Executive \nVote Share Margin") +
  ylab("Total Debt \n(per capita, logged)") +
  theme_bw() +
  theme(axis.title.x  = element_text(size=16, margin = margin(t = 16)),
        axis.text.x  = element_text(size=12),
        axis.title.y  = element_text(size=16, margin = margin(r = 16)),
        axis.text.y  = element_text(size=12),
        legend.position = "none") 



## return figure
rd_plot_debt_log

## save figure
ggsave(rd_plot_debt_log, filename = "fig_c4b_replication_rd_plot_debt_log.pdf", width = 5, height = 4)


## Table C.7
print.xtable(xtable(select(tab_alt_specs_debt, outcome, base_coef, base_robust_se, base_robust_pval, base_h,
                           covs_coef, covs_robust_se, covs_robust_pval, covs_h, dv_form),
                    digits = c(3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0)), hline.after = NULL,
                 only.contents = TRUE, include.colnames = FALSE, include.rownames = FALSE,
             file = "tab_c7_replication_table_results_alt_specs_debt_RR.tex")


## Figure C.5
## generate figures
plot_ttl_debt_multi_logs <- plot_ttl_own_revs_multi %+%
  filter(gg_multi_bandwidth, outcome == "ttl_debt",  dv_form == "Dollars (per capita, logged)") +
  geom_point(data = filter(gg_rdrobust, outcome == "ttl_debt", dv_form == "Dollars (per capita, logged)"), aes(x = h, y = coef), shape = 5, size = 2.5) +
  geom_errorbar(data = filter(gg_rdrobust, outcome == "ttl_debt", dv_form == "Dollars (per capita, logged)"),
                aes(ymin = ci_low, ymax = ci_upp), width = 0, linetype = "dashed") +
  scale_y_continuous( breaks = seq(-1, 2, 1), labels = scale_fun_revs) +
  coord_cartesian(ylim = c(-1.5, 2.5)) +
  ylab("Effect on Total Debt \n(per capita, logged)") 


plot_ttl_debt_multi_dollars <- plot_ttl_own_revs_multi %+%
  filter(gg_multi_bandwidth, outcome == "ttl_debt",  dv_form == "Dollars (per capita)") +
  geom_point(data = filter(gg_rdrobust, outcome == "ttl_debt", dv_form == "Dollars (per capita)"), aes(x = h, y = coef), shape = 5, size = 2.5) +
  geom_errorbar(data = filter(gg_rdrobust, outcome == "ttl_debt", dv_form == "Dollars (per capita)"),
                aes(ymin = ci_low, ymax = ci_upp), width = 0, linetype = "dashed") +
  scale_y_continuous( breaks = seq(-1000, 2500, 250), labels = dollar) +
  coord_cartesian(ylim = c(-1050, 2550)) +
  ylab("Effect on Total Debt \n(per capita)") 


plot_ttl_debt_multi_diffs <- plot_ttl_own_revs_multi %+%
  filter(gg_multi_bandwidth, outcome == "ttl_debt",  dv_form == "Diffference in Dollars (per capita)") +
  geom_point(data = filter(gg_rdrobust, outcome == "ttl_debt", dv_form == "Diffference in Dollars (per capita)"), aes(x = h, y = coef), shape = 5, size = 2.5) +
  geom_errorbar(data = filter(gg_rdrobust, outcome == "ttl_debt", dv_form == "Diffference in Dollars (per capita)"),
                aes(ymin = ci_low, ymax = ci_upp), width = 0, linetype = "dashed") +
  scale_y_continuous( breaks = seq(-1000, 2500, 250), labels = dollar) +
  coord_cartesian(ylim = c(-1050, 2550)) +
  ylab("Effect on Total Debt \n(per capita difference)")

## return figures
plot_ttl_debt_multi_dollars
plot_ttl_debt_multi_diffs
plot_ttl_debt_multi_logs

## save figures
ggsave(plot_ttl_debt_multi_logs, filename = "fig_c5_replication_plot_ttl_debt_multi_logs.pdf", height = 4, width = 12)
ggsave(plot_ttl_debt_multi_dollars, filename = "fig_c5_replication_plot_ttl_debt_multi_dollars.pdf", height = 4, width = 12)
ggsave(plot_ttl_debt_multi_diffs, filename = "fig_c5_replication_plot_ttl_debt_multi_diffs.pdf", height = 4, width = 12)



#### Section D - Spending
## Table D.8
print.xtable(xtable(select(filter(tab_alt_specs_quad, outcome_class == "spending", specs == "CCT covs"), outcome, coef, robust_se, robust_pval, h),
                    digits = c(3, 3, 3, 3, 3, 3)), hline.after = NULL,
             only.contents = TRUE, include.colnames = FALSE, include.rownames = FALSE,
             add.to.row = list(pos = list(1:9), command = c("\\\\\n")),
             file = "tab_d8_replication_table_quadratic_results_spending_RR.tex")

## Table D.9
print.xtable(xtable(select(tab_alt_specs_spending, outcome, base_coef, base_robust_se, base_robust_pval, base_h,
                           covs_coef, covs_robust_se, covs_robust_pval, covs_h, dv_form),
                    digits = c(3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0)), hline.after = NULL,
             only.contents = TRUE, include.colnames = FALSE, include.rownames = FALSE,
             add.to.row = list(pos = list(seq(3, 27, 3)), command = c("\\\\\n")),
             file = "tab_d9_replication_table_results_alt_specs_spending_RR.tex")


## Table D.10
print.xtable(xtable(select(tab_alt_specs_spending_category, outcome, base_coef, base_robust_se, base_robust_pval, base_h,
                           covs_coef, covs_robust_se, covs_robust_pval, covs_h, dv_form),
                    digits = c(3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0)), hline.after = NULL,
             only.contents = TRUE, include.colnames = FALSE, include.rownames = FALSE,
             add.to.row = list(pos = list(seq(3, 6, 3)), command = c("\\\\\n")),
             file = "tab_d10_replication_table_results_alt_specs_spending_category_RR.tex")


## Figure D.6(a)
## generate figures
plot_admin_multi_logs <- plot_ttl_own_revs_multi %+%
  filter(gg_multi_bandwidth, outcome == "admin",  dv_form == "Dollars (per capita, logged)") +
  geom_point(data = filter(gg_rdrobust, outcome == "admin", dv_form == "Dollars (per capita, logged)"), aes(x = h, y = coef), shape = 5, size = 2.5) +
  geom_errorbar(data = filter(gg_rdrobust, outcome == "admin", dv_form == "Dollars (per capita, logged)"),
                aes(ymin = ci_low, ymax = ci_upp), width = 0, linetype = "dashed") +
  scale_y_continuous( breaks = seq(-4, 2, 2), labels = scale_fun) +
  coord_cartesian(ylim = c(-4.5, 2.5)) +
  ylab("Effect on Administration \n(per capita, logged)") 


plot_admin_multi_dollars <- plot_ttl_own_revs_multi %+%
  filter(gg_multi_bandwidth, outcome == "admin",  dv_form == "Dollars (per capita)") +
  geom_point(data = filter(gg_rdrobust, outcome == "admin", dv_form == "Dollars (per capita)"), aes(x = h, y = coef), shape = 5, size = 2.5) +
  geom_errorbar(data = filter(gg_rdrobust, outcome == "admin", dv_form == "Dollars (per capita)"),
                aes(ymin = ci_low, ymax = ci_upp), width = 0, linetype = "dashed") +
  scale_y_continuous( breaks = seq(-200, 250, 100), labels = dollar) +
  coord_cartesian(ylim = c(-250, 250)) +
  ylab("Effect on Administration \n(per capita)") 


plot_admin_multi_diffs <- plot_ttl_own_revs_multi %+%
  filter(gg_multi_bandwidth, outcome == "admin",  dv_form == "Diffference in Dollars (per capita)") +
  geom_point(data = filter(gg_rdrobust, outcome == "admin", dv_form == "Diffference in Dollars (per capita)"), aes(x = h, y = coef), shape = 5, size = 2.5) +
  geom_errorbar(data = filter(gg_rdrobust, outcome == "admin", dv_form == "Diffference in Dollars (per capita)"),
                aes(ymin = ci_low, ymax = ci_upp), width = 0, linetype = "dashed") +
  scale_y_continuous( breaks = seq(-200, 250, 100), labels = dollar) +
  coord_cartesian(ylim = c(-250, 250)) +
  ylab("Effect on Administration \n(per capita difference)")


## return figures
plot_admin_multi_dollars
plot_admin_multi_diffs
plot_admin_multi_logs

## save figures
ggsave(plot_admin_multi_logs, filename = "fig_d6a_replication_plot_admin_multi_logs.pdf", height = 4, width = 12)
ggsave(plot_admin_multi_dollars, filename = "fig_d6a_replication_plot_admin_multi_dollars.pdf", height = 4, width = 12)
ggsave(plot_admin_multi_diffs, filename = "fig_d6a_replication_plot_admin_multi_diffs.pdf", height = 4, width = 12)



## Figure D.6(b)
## generate figures
plot_roads_multi_logs <- plot_ttl_own_revs_multi %+%
  filter(gg_multi_bandwidth, outcome == "roads",  dv_form == "Dollars (per capita, logged)") +
  geom_point(data = filter(gg_rdrobust, outcome == "roads", dv_form == "Dollars (per capita, logged)"), aes(x = h, y = coef), shape = 5, size = 2.5) +
  geom_errorbar(data = filter(gg_rdrobust, outcome == "roads", dv_form == "Dollars (per capita, logged)"),
                aes(ymin = ci_low, ymax = ci_upp), width = 0, linetype = "dashed") +
  scale_y_continuous( breaks = seq(-4, 2, 2), labels = scale_fun) +
  coord_cartesian(ylim = c(-4.5, 2.5)) +
  ylab("Effect on Roads \n(per capita, logged)") 


plot_roads_multi_dollars <- plot_ttl_own_revs_multi %+%
  filter(gg_multi_bandwidth, outcome == "roads",  dv_form == "Dollars (per capita)") +
  geom_point(data = filter(gg_rdrobust, outcome == "roads", dv_form == "Dollars (per capita)"), aes(x = h, y = coef), shape = 5, size = 2.5) +
  geom_errorbar(data = filter(gg_rdrobust, outcome == "roads", dv_form == "Dollars (per capita)"),
                aes(ymin = ci_low, ymax = ci_upp), width = 0, linetype = "dashed") +
  scale_y_continuous( breaks = seq(-200, 250, 100), labels = dollar) +
  coord_cartesian(ylim = c(-250, 250)) +
  ylab("Effect on Roads \n(per capita)") 


plot_roads_multi_diffs <- plot_ttl_own_revs_multi %+%
  filter(gg_multi_bandwidth, outcome == "roads",  dv_form == "Diffference in Dollars (per capita)") +
  geom_point(data = filter(gg_rdrobust, outcome == "roads", dv_form == "Diffference in Dollars (per capita)"), aes(x = h, y = coef), shape = 5, size = 2.5) +
  geom_errorbar(data = filter(gg_rdrobust, outcome == "roads", dv_form == "Diffference in Dollars (per capita)"),
                aes(ymin = ci_low, ymax = ci_upp), width = 0, linetype = "dashed") +
  scale_y_continuous( breaks = seq(-200, 250, 100), labels = dollar) +
  coord_cartesian(ylim = c(-250, 250)) +
  ylab("Effect on Roads \n(per capita difference)")


## return figures
plot_roads_multi_dollars
plot_roads_multi_diffs
plot_roads_multi_logs

## save figures
ggsave(plot_roads_multi_logs, filename = "fig_d6b_replication_plot_roads_multi_logs.pdf", height = 4, width = 12)
ggsave(plot_roads_multi_dollars, filename = "fig_d6b_replication_plot_roads_multi_dollars.pdf", height = 4, width = 12)
ggsave(plot_roads_multi_diffs, filename = "fig_d6b_replication_plot_roads_multi_diffs.pdf", height = 4, width = 12)


## Figure D.6(c)
## generate figures
plot_housing_multi_logs <- plot_ttl_own_revs_multi %+%
  filter(gg_multi_bandwidth, outcome == "housing",  dv_form == "Dollars (per capita, logged)") +
  geom_point(data = filter(gg_rdrobust, outcome == "housing", dv_form == "Dollars (per capita, logged)"), aes(x = h, y = coef), shape = 5, size = 2.5) +
  geom_errorbar(data = filter(gg_rdrobust, outcome == "housing", dv_form == "Dollars (per capita, logged)"),
                aes(ymin = ci_low, ymax = ci_upp), width = 0, linetype = "dashed") +
  scale_y_continuous( breaks = seq(-4, 2, 2), labels = scale_fun) +
  coord_cartesian(ylim = c(-4.5, 2.5)) +
  ylab("Effect on Housing & Comm. Development \n(per capita, logged)") 


plot_housing_multi_dollars <- plot_ttl_own_revs_multi %+%
  filter(gg_multi_bandwidth, outcome == "housing",  dv_form == "Dollars (per capita)") +
  geom_point(data = filter(gg_rdrobust, outcome == "housing", dv_form == "Dollars (per capita)"), aes(x = h, y = coef), shape = 5, size = 2.5) +
  geom_errorbar(data = filter(gg_rdrobust, outcome == "housing", dv_form == "Dollars (per capita)"),
                aes(ymin = ci_low, ymax = ci_upp), width = 0, linetype = "dashed") +
  scale_y_continuous( breaks = seq(-200, 250, 100), labels = dollar) +
  coord_cartesian(ylim = c(-250, 250)) +
  ylab("Effect on Housing & Comm. Development \n(per capita)") 


plot_housing_multi_diffs <- plot_ttl_own_revs_multi %+%
  filter(gg_multi_bandwidth, outcome == "housing",  dv_form == "Diffference in Dollars (per capita)") +
  geom_point(data = filter(gg_rdrobust, outcome == "housing", dv_form == "Diffference in Dollars (per capita)"), aes(x = h, y = coef), shape = 5, size = 2.5) +
  geom_errorbar(data = filter(gg_rdrobust, outcome == "housing", dv_form == "Diffference in Dollars (per capita)"),
                aes(ymin = ci_low, ymax = ci_upp), width = 0, linetype = "dashed") +
  scale_y_continuous( breaks = seq(-200, 250, 100), labels = dollar) +
  coord_cartesian(ylim = c(-250, 250)) +
  ylab("Effect on Housing & Comm. Development \n(per capita difference)")



## return figures
plot_housing_multi_dollars
plot_housing_multi_diffs
plot_housing_multi_logs

## save figures
ggsave(plot_housing_multi_logs, filename = "fig_d6c_replication_plot_housing_multi_logs.pdf", height = 4, width = 12)
ggsave(plot_housing_multi_dollars, filename = "fig_d6c_replication_plot_housing_multi_dollars.pdf", height = 4, width = 12)
ggsave(plot_housing_multi_diffs, filename = "fig_d6c_replication_plot_housing_multi_diffs.pdf", height = 4, width = 12)



## Table D.11
print.xtable(xtable(select(tab_alt_specs_spending_shares, outcome, base_coef, base_robust_se, base_robust_pval, base_h,
                           covs_coef, covs_robust_se, covs_robust_pval, covs_h, dv_form),
                    digits = c(3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0)), hline.after = NULL,
             only.contents = TRUE, include.colnames = FALSE, include.rownames = FALSE,
             add.to.row = list(pos = list(seq(2, 18, 2)), command = c("\\\\\n")),
             file = "tab_d11_replication_table_results_alt_specs_spending_shares_RR.tex")


## Table E.12
print.xtable(xtable(select(filter(main_results_rdrobust, specs == "covs", outcome_class == "size"), outcome, coef, ci, p_val, h, n), digits = c(3,3, 3, 3, 3, 3, 0)), hline.after = NULL,
                 only.contents = TRUE, include.colnames = FALSE, include.rownames = FALSE,
             add.to.row = list(pos = list(1:9), command = c("\\\\\n")),
             file = "tab_e12_replication_table_results_size_rdrobust_covs_RR.tex")




## Table E.13
print.xtable(xtable(select(filter(main_results_rdrobust, specs == "covs", outcome_class == "spending"), outcome, coef, ci, p_val, h, n), digits = c(3,3, 3, 3, 3, 3, 0)), hline.after = NULL,
             only.contents = TRUE, include.colnames = FALSE, include.rownames = FALSE,
             add.to.row = list(pos = list(1:9), command = c("\\\\\n")),
             file = "tab_e13_replication_table_results_spending_rdrobust_covs_RR.tex")




## Table F.14
print.xtable(xtable(select(filter(tab_alt_specs_size_party, candidate_type == "partisan_rep"), outcome, base_coef, base_robust_se, base_robust_pval, base_h,
                           covs_coef, covs_robust_se, covs_robust_pval, covs_h, dv_form),
                    digits = c(3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0)), hline.after = NULL,
             only.contents = TRUE, include.colnames = FALSE, include.rownames = FALSE,
             add.to.row = list(pos = list(seq(3, 27, 3)), command = c("\\\\\n")),
             file = "tab_f14_replication_table_two_party_results_alt_specs_size_RR.tex")


## Table F.15
print.xtable(xtable(select(filter(tab_alt_specs_spending_party, candidate_type == "partisan_rep"), outcome, base_coef, base_robust_se, base_robust_pval, base_h,
                           covs_coef, covs_robust_se, covs_robust_pval, covs_h, dv_form),
                    digits = c(3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0)), hline.after = NULL,
             only.contents = TRUE, include.colnames = FALSE, include.rownames = FALSE,
             add.to.row = list(pos = list(seq(3, 27, 3)), command = c("\\\\\n")),
             file = "tab_f15_replication_table_two_party_results_alt_specs_spending_RR.tex")


## Table F.16
print.xtable(xtable(select(filter(tab_alt_specs_spending_category_party, candidate_type == "partisan_rep"), outcome, base_coef, base_robust_se, base_robust_pval, base_h,
                           covs_coef, covs_robust_se, covs_robust_pval, covs_h, dv_form),
                    digits = c(3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0)), hline.after = NULL,
             only.contents = TRUE, include.colnames = FALSE, include.rownames = FALSE,
             add.to.row = list(pos = list(seq(3, 6, 3)), command = c("\\\\\n")),
             file = "tab_f16_replication_table_two_party_results_alt_specs_spending_category_RR.tex")


## Figure G.7(a)
## generate figure
plot_size_govt_fog <- ggplot(filter(tab_alt_specs_FOG, outcome_class == "size", specs == "dollars", form_govt != "Non-Mayor-Council"),
                             aes(x = covs_coef, y = fct_rev(outcome), group = form_govt)) +
  geom_point(aes(x = covs_coef, y = fct_rev(outcome), color = form_govt, shape = form_govt), position = position_dodgev(height = .5), size = 2.5) +
  geom_errorbarh(aes(xmin = covs_coef - (-qnorm((1-0.90)/2) * covs_robust_se), xmax = covs_coef + (-qnorm((1-0.90)/2) * covs_robust_se), 
                     color = form_govt), size = 1, position = position_dodgev(height = .5), height = 0) +
  geom_errorbarh(aes(xmin = covs_coef - (-qnorm((1-0.95)/2) * covs_robust_se), xmax = covs_coef + (-qnorm((1-0.95)/2) * covs_robust_se), 
                     color = form_govt), position = position_dodgev(height = .5), height = 0, linetype = "dashed") +
  scale_color_manual(values = c("black", "red", "grey")) +
  facet_grid(outcome_type ~ ., scales = "free_y", space = "free_y") +
  geom_vline(xintercept = 0, linetype = "dashed", colour = "grey") +
  scale_x_continuous(breaks = seq(-750, 750, 250), labels = dollar) +
  xlab("\n Effect of Electing a \nBusiness Owner or Executive") +
  ylab("") +
  coord_cartesian(xlim = c(-1000, 1000)) +
  theme_bw() +
  theme(axis.title.x = element_text(size = 12),
        axis.text.x = element_text(size = 10),
        axis.text.y = element_text(size = 12),
        strip.text.y = element_text(size = 8),
        legend.position = "bottom",
        legend.title = element_blank())

## return figure
plot_size_govt_fog

## save figure
ggsave(plot_size_govt_fog, filename = "fig_g7a_replication_plot_results_size_govt_form_govt_RR.pdf", height = 8, width = 6)


## Figure G.7(b)
## generate figure
plot_spending_fog <- ggplot(filter(tab_alt_specs_FOG, outcome_class == "spending" | outcome_class == "spending category", specs == "dollars", form_govt != "Non-Mayor-Council"),
                            aes(x = covs_coef, y = fct_rev(outcome), group = form_govt))  +
  geom_point(aes(x = covs_coef, y = fct_rev(outcome), color = form_govt, shape = form_govt), position = position_dodgev(height = .5), size = 2.5) +
  geom_errorbarh(aes(xmin = covs_coef - (-qnorm((1-0.90)/2) * covs_robust_se), xmax = covs_coef + (-qnorm((1-0.90)/2) * covs_robust_se), 
                     color = form_govt), size = 1, position = position_dodgev(height = .5), height = 0) +
  geom_errorbarh(aes(xmin = covs_coef - (-qnorm((1-0.95)/2) * covs_robust_se), xmax = covs_coef + (-qnorm((1-0.95)/2) * covs_robust_se), 
                     color = form_govt), position = position_dodgev(height = .5), height = 0, linetype = "dashed") +
  scale_color_manual(values = c("black", "red", "grey")) +
  facet_grid(outcome_type ~ ., scales = "free_y", space = "free_y") +
  geom_vline(xintercept = 0, linetype = "dashed", colour = "grey") +
  scale_x_continuous(breaks = seq(-100, 100, 50), labels = dollar) +
  xlab("\n Effect of Electing a \nBusiness Owner or Executive") +
  ylab("") +
  coord_cartesian(xlim = c(-200, 200)) +
  theme_bw() +
  theme(axis.title.x = element_text(size = 12),
        axis.text.x = element_text(size = 10),
        axis.text.y = element_text(size = 12),
        strip.text.y = element_text(size = 10),
        legend.position = "bottom",
        legend.title = element_blank())


## return figure
plot_spending_fog

## save figure
ggsave(plot_spending_fog, filename = "fig_g7b_replication_plot_results_spending_form_govt_RR.pdf", height = 8, width = 6)


## Table G.17
print.xtable(xtable(select(filter(tab_alt_specs_size_FOG, form_govt == "Mayor-Council"), outcome, covs_coef, covs_robust_se, covs_robust_pval, covs_h),
                    digits = c(3, 3, 3, 3, 3, 3)), hline.after = NULL,
             only.contents = TRUE, include.colnames = FALSE, include.rownames = FALSE,
             add.to.row = list(pos = list(1:9), command = c("\\\\\n")),
             file = "tab_g17_replication_table_mayor_council_results_size_RR.tex")

## Table G.18
print.xtable(xtable(select(filter(tab_alt_specs_size_FOG, form_govt == "Council-Manager"), outcome, covs_coef, covs_robust_se, covs_robust_pval, covs_h),
                    digits = c(3, 3, 3, 3, 3, 3)), hline.after = NULL,
             only.contents = TRUE, include.colnames = FALSE, include.rownames = FALSE,
             add.to.row = list(pos = list(1:9), command = c("\\\\\n")),
             file = "tab_g18_replication_table_council_manager_results_size_RR.tex")


## Table G.19
print.xtable(xtable(select(filter(tab_alt_specs_spending_FOG, form_govt == "Mayor-Council"), outcome, covs_coef, covs_robust_se, covs_robust_pval, covs_h),
                    digits = c(3, 3, 3, 3, 3, 3)), hline.after = NULL,
             only.contents = TRUE, include.colnames = FALSE, include.rownames = FALSE,
             add.to.row = list(pos = list(1:9), command = c("\\\\\n")),
             file = "tab_g19_replication_table_mayor_council_results_spending_RR.tex")


## Table G.20
print.xtable(xtable(select(filter(tab_alt_specs_spending_FOG, form_govt == "Council-Manager"), outcome, covs_coef, covs_robust_se, covs_robust_pval, covs_h),
                    digits = c(3, 3, 3, 3, 3, 3)), hline.after = NULL,
             only.contents = TRUE, include.colnames = FALSE, include.rownames = FALSE,
             add.to.row = list(pos = list(1:9), command = c("\\\\\n")),
             file = "tab_g20_replication_table_council_manager_results_spending_RR.tex")


